# 打开文件
# f = open("f:/homework/computer/Java/test.txt", 'r')
# print(f)
# print(type(f))

# 关闭文件
# alist = []
# count = 0
# while True:
#     f = open("f:/homework/computer/Java/test.txt", 'r')
#     alist.append(f)
#     f.close()
#     count += 1
#     print(f'打开文件的个数: {count}')


# # 写文件
# f = open("f:/homework/computer/Java/test.txt", 'w')
# f.write('hello')
# f.close()

# # 使用a操作进行追加
# f = open('f:/homework/computer/Java/test.txt', 'a')
# f.write('hello bit')
# f.close()

# # 读文件
# f = open("f:/homework/computer/Java/test.txt", 'r')
# result = f.read(2)
# print(result)
# result = f.read()
# print(result)
# f.close()


# # for循环读取多行内容
# f = open('f:/homework/computer/Java/test.txt', 'r', encoding='utf8')
# for line in f:
#     print(f'line = {line}', end='')
# f.close()


# # readlines读取整个文件内容
# f = open('f:/homework/computer/Java/test.txt', 'r', encoding='utf-8')
# lines = f.readlines()
# print(lines)
# f.close()

# 使用上下文管理器 帮助程序员自动关闭文件
with open('f:/homework/computer/Java/test.txt', 'r', encoding='utf8') as f:
    lines = f.readlines()
    print(lines)


